<?php

/**
 * 文本回复
 * 
 * Apps/Common/Service/WxTextReplyService.class.php
 * Zix @ 2015-07-16
 */

namespace Common\Service;

class WxTextReplyService {

  private static $instance;

  var $type = array(
    'text'      => '文本回复',
    'subscribe' => '订阅回复'
  );
  
  var $status = array(
    0 => '禁用' ,
    1 => '启用'
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxTextReplyService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'type' => text ,
      'keyword' => '',
      'content' => '',
    );
  }

  function get_by_id($id) {
    $WxTextReply = M('WxTextReply');
    $data = $WxTextReply->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'wx_id' => '' ,
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxTextReply = M('WxTextReply');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['wx_id'])) {
      $where['wx_id'] = $config['wx_id'];
    }

    if ($config['count']) {
      return $WxTextReply->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxTextReply
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxTextReply->_sql();
      return $data ? $data : array();
    }
  }

  function create( $data ) {
    $WxTextReply = M('WxTextReply');
    
    if( $data['type'] == 'subscribe' ) {
      $check_content = $this->get_by_subscribe($data['wx_id']);
      if ( !empty( $check_content ) ) {
        return ajax_arr('已经添加过关注回复了');
      }
    } else {
      if( empty( $data['keyword'] ) ) {
        return ajax_arr('请添加关键字');
      }
      $check_content = $this->get_by_keyword($data['wx_id'] , $data['keyword']);
      if ( !empty( $check_content ) ) {
        return ajax_arr('该关键字已经添加过了');
      }
    }
    
    $ret = $WxTextReply->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $WxTextReply = M('WxTextReply');
    
    if ( $data['type'] == 'subscribe' ) {
      $old_data = $this->get_by_type( $data['wx_id'] , $data['type']);
      if ( !empty( $old_data ) && $old_data['id'] != $id ) {
        return ajax_arr('已经添加过关注回复了');
      }      
    } else {
      if( empty( $data['keyword'] ) ) {
        return ajax_arr('请添加关键字');
      }
      $old_data = $this->get_by_keyword( $data['wx_id'] , $data['keyword'] );
      if ( !empty( $old_data ) && $old_data['id'] != $id ) {
        return ajax_arr('该关键字已经添加过回复了');
      }
    }
    
    $ret = $WxTextReply->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxTextReply = M('WxTextReply');
    $ret = $WxTextReply->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  /**
   * 按类型获取
   * @param type $wx_id
   * @param type $type
   * @return type
   */
  function get_by_type( $wx_id , $type = 'text' , $id = '') {
    $WxTextReply = M('WxTextReply');
    $where = array(
      'wx_id' => $wx_id ,
      'type' => $type ,
    );
    
    if ( !empty( $id ) ) {
      $where['id'] = $id ;
    }
    $data = $WxTextReply->where($where)->find();
    return $data ? $data : array();
  }
  
  /**
   * 根据关键字获取
   * @param type $wx_id
   * @param type $keyword
   * @return string
   */
  function get_by_keyword( $wx_id , $keyword ) {
    $WxTextReply = M('WxTextReply');
    $where = array(
      'wx_id' => $wx_id ,
      'keyword' => $keyword ,
    );
    $data = $WxTextReply->where($where)->find();
    return $data ? $data : array();
  }
    
  
  function get_content_by_keyword( $wx_id , $keyword ) {
    $data = $this->get_by_keyword($wx_id, $keyword);
    return empty( $data ) ? '' : $data['content'] ;
  }


  /**
   * 获取关注文字
   * @param type $wx_id
   * @return string
   */
  function get_by_subscribe( $wx_id ) {
    $WxTextReply = M('WxTextReply');
    $where = array(
      'wx_id' => $wx_id ,
      'type' => 'subscribe',
    );
    $data = $WxTextReply->where($where)->find();
    if ( !$data || empty( $data ) ) {
      return '' ;
    } else {
      return $data['content'] ;
    }
  }
  
}
